package com.heyqing.topic.PlusOne_0066;

import java.util.Arrays;
import java.util.Map;
import java.util.Scanner;

/**
 * ClassName:PlusOne
 * Package:com.heyqing.topic.PlusOne_0066
 * Description:
 *
 * @Date:2024/9/10
 * @Author:Heyqing
 */
public class PlusOne {
    public int[] plusOne(int[] digits) {
        int last = digits[digits.length - 1] + 1;
        if (last <= 9) {
            digits[digits.length - 1] = last;
            return digits;
        }
        int flag = 1;
        int[] res = new int[digits.length + 1];
        res[digits.length] = 0;
        int count = res.length - 2;
        for (int i = digits.length - 2; i >= 0; i--) {
            int add1Number = digits[i] + flag;
            if (add1Number > 9) {
                flag = 1;
                res[count--] = 0;
            } else {
                flag = 0;
                res[count--] = add1Number;
            }
        }
        if (res[1] == 0){
            res[count] += 1;
        }else {
            return Arrays.copyOfRange(res,1,res.length);
        }
        return res;
    }

    public static void start() {
        Scanner sc = new Scanner(System.in);
        System.out.println("数组长度:");
        int len = sc.nextInt();
        int[] digits = new int[len];
        for (int i = 0; i < len; i++) {
            System.out.print("第" + (i + 1) + "个：");
            digits[i] = sc.nextInt();
        }
        PlusOne plusOne = new PlusOne();
        int[] ints = plusOne.plusOne(digits);
        System.out.print("结果：");
        for (int i = 0; i < ints.length; i++) {
            System.out.print(ints[i] + " ");
        }
    }
}
